package com.graduate.twentyone.controller;


import com.graduate.twentyone.common.R;
import com.graduate.twentyone.domain.RestDate;
import com.graduate.twentyone.domain.Users;
import com.graduate.twentyone.mapper.UsersMapper;

import com.graduate.twentyone.service.UsersService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@CrossOrigin
public class UsersController {


    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UsersService usersService;

    @RequestMapping(value = "userlogin", method = RequestMethod.POST)
//    @GetMapping("/userlogin")
    public RestDate userslogin(@RequestBody Users users,HttpSession session) {
        String uname = users.getUname();
        String pwd = users.getPwd();
//        System.out.println("  222  uname  " + uname);
//        System.out.println("  333  pwd  " + pwd);

        Subject subject = SecurityUtils.getSubject();

        RestDate<Object> restDate = new RestDate<>(800, "error", "返回的数据");
        String authToken1 = (String) subject.getSession().getId();

        System.out.println(authToken1 + "    authtoken");
        Map map = new HashMap();
        map.put("authToken", authToken1);
        try {
            subject.login(new UsernamePasswordToken(uname, pwd));
        } catch (IncorrectCredentialsException incorrectCredentialsException) {
            restDate = new RestDate<>(2, "密码错误", "返回的数据");
            return restDate;
        } catch (UnknownAccountException UnknownAccountException) {
            restDate = new RestDate<>(1, "账号错误", "返回的数据");
            return restDate;
        } catch (NullPointerException nullPointerException) {
            restDate = new RestDate<>(0, "未输入信息", "返回的数据");
            return restDate;
        }
        if (subject.isAuthenticated()) {
            String loginrname = usersMapper.loginrname(uname);
            Users login = usersMapper.login(uname);
            users.setOnline(1);
            usersMapper.setOnline(users);
            session.setAttribute("uname",users.getUname());
            System.out.println(loginrname + "   身份");
            map.put("loginrname", loginrname);
            map.put("umess", login);
            restDate = new RestDate<>(-1, "success", map);
            return restDate;
        }

        return restDate;
    }

    @RequestMapping(value = "/userquit", method = RequestMethod.POST)
    public int userquit(HttpSession session,Users users) {
        users.setOnline(0);
        users.setUname((String) session.getAttribute("uname"));
        usersMapper.setOnline(users);
        session.invalidate();
        System.out.println("退出登录");
        return 1;
    }

    // 注册
    @RequiresPermissions("admin:all")
    @PostMapping("/reg")
    public R addOne(@RequestBody Users users) {
        Integer integer = usersService.addOne(users);
        System.out.println("usersRRRRRR = " + users.getRid());
        Users selectooo = usersMapper.selectooo();
        System.out.println("selectooo.getUid() = " + selectooo.getUid());
        usersService.addPremiss(selectooo.getUid(), users.getRid());
        return R.ok().data("ans", integer);
    }

    //用户权限设置接口
    @RequiresPermissions("admin:all")
    @PostMapping("/premiss")
    public R premissSet(@RequestBody Users users) {
        Boolean ans;
        if (usersService.queryPremiss(users.getUid()) != null) {
            ans = usersService.updatePremiss(users.getRid(), users.getUid());
        } else
            ans = usersService.addPremiss(users.getUid(), users.getRid());
        return R.ok().data("res", ans);
    }

    //删除当前用户
    @RequiresPermissions("admin:all")
    @DeleteMapping("/deluser")
    public R delUser(@RequestBody Users users) {
        return R.ok().data("res", usersService.deleteOneUser(users.getUid()));
    }


    //    修改信息
    @RequiresPermissions("admin:all")
    @PutMapping("/updateUser")
    public R updateUser(@RequestBody Users users) {
        System.out.println("users.getRid() = " + users.getRid());
        System.out.println("users.getUid() = " + users.getUid());
        Boolean aBoolean = usersService.updatePremiss(users.getRid(), users.getUid());
        System.out.println("aBoolean = " + aBoolean);
        return R.ok().data("res", usersService.updateUser(users));
    }

    //    查询用户
    @RequiresPermissions("admin:all")
    @GetMapping("/selectUsers")
    public R selectUser() {
        return R.ok().data("select", usersService.selectUsers());
    }


//    查询在线状态
    @RequiresPermissions("client:select")
    @GetMapping("/selectClient")
    public  R selectOnlineStatus(){

        List<Users> users = usersService.selectOnlineStatus();
        for (Users users1:users){
            System.out.println("uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuusers1 = " + users1.getOnline());
        }


        return R.ok().data("Selectclient",usersService.selectOnlineStatus());
    }
}

